<?php

namespace App\Http\Controllers\Index;

use App\Model\RedisKey;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use DB;

class IndexController extends Controller
{
    //测试
    public function index(){
        $data = User::getInstance()->getUserInfo(18);
        echo '<pre>';
//        var_dump(objToArr($data));
        var_dump($data);
    }
    public function redis(){
//        Redis::set('name', 'Taylor');
//        $val = Redis::get('name');
//        var_dump($val);
        $key = sprintf(RedisKey::$user_info,1);
        return $key;
    }
    public function strLength(){
//        return User::getInstance()->learn();
        $str = '我还是喜欢你1';
//        return mb_strlen($str);
        if(mb_strlen($str) > 6){
            return 1;
        }else{
            return 2;
        }
    }

    public function qrCode(){
//        return QrCode::color(255,0,255)->encoding('UTF-8')->generate('SimpleQrCode扩展包生成二维码测试！');
        return QrCode::size(200)->color(255,0,255)->backgroundColor(125,245,0)->encoding('UTF-8')->generate('SimpleQrCode扩展包生成二维码测试！');

    }
    public function session(Request $request){
//        session(['key' => 'value']);
//        $data = $request->session()->all();
        $value = $request->session()->get('key');
        echo '<pre>';
        var_dump($value);
    }

    public function cache(){
//        Cache::put('bar', 'baz', 60); // 1 分钟
//        $value = Cache::get('bar');
//        echo '<pre>';
//        var_dump($value);

        Cache::store('redis')->put('bars', 'baz1111', 600); // 10 分钟
        $value = Cache::store('redis')->get('bars');
        echo '<pre>';
        var_dump($value);
    }

    public function url(){
        return storage_path();
    }
    public function test(){
        if(is_file('./02.txt')){
            $num = file_get_contents('./02.txt');
            $num+=1;
            file_put_contents('./02.txt',$num);
        }else{
            file_put_contents('./02.txt',1);
        }
        $val = file_get_contents('./02.txt');
//        file_put_contents('./01.txt','我还是喜欢你');
        //$val = file_get_contents('./01.txt');
        var_dump($val);
    }

    public function log(){
        log::info('test');
    }

    public function file(){
        if(is_file('./01.txt')){
            $num = file_get_contents('./01.txt');
            $num+=1;
            file_put_contents('./01.txt',$num);
        }else{
            file_put_contents('./01.txt',1);
        }
    }
    public function __yield($number){
        $data = [];
        for($i=0;$i<$number;$i++){
            $data[] = time();
        }
        return $data;
    }
    public function ___yield($number){
        for($i=0;$i<$number;$i++){
            yield time();
        }
    }
    public function yield(){
/*        $result = $this->__yield(10);
        foreach($result as $value){
            sleep(1);//这里停顿1秒，我们后续有用
            echo $value.'<br />';
        }*/

        //一般读取大文件时候会用到
        /*
            $result = $this->__yield(10);
            foreach($result as $value){
                sleep(1);
                echo $value.'<br />';
            }
            var_dump($result);
        */

    }
    //tyr——catch
    public function tryCatch(){
        DB::beginTransaction();
        try{
            $del1 = DB::table("test")->where('id',15)->delete();
            if(!$del1) throw new \Exception("del1删除失败");
            $del2 = DB::table("test")->where('id',17)->delete();
            if(!$del2) throw new \Exception("del2删除失败");
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            echo $e->getMessage();
            echo $e->getCode();
            return "201";
        }
    }
    public function add(){
        return "this is add";
    }
    public function edit(){
        return "this is edit";
    }
   

}
